package com.cxyzy.demo.tween_anims

import android.os.Bundle
import android.view.View
import android.view.View.INVISIBLE
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.ScaleAnimation
import android.view.animation.TranslateAnimation
import androidx.appcompat.app.AppCompatActivity
import com.cxyzy.demo.R
import kotlinx.android.synthetic.main.activity_scale_down_move.*


/**
 * 组合动画样例：缩小控件并移动到目标控件,然后消失
 * 参考资料：
 * https://blog.csdn.net/qq_38039015/article/details/80671222
 * https://blog.csdn.net/qq_39056803/article/details/79375667
 * https://blog.csdn.net/qq_40881680/article/details/82378391
 */
class ScaleDownAndMoveActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scale_down_move)
        initView()
    }

    private fun initView() {
        toMoveScaleDownBtn.setOnClickListener { scaleDownAndMove(it, destBtn) }
    }

    private fun scaleDownAndMove(srcView: View, destView: View) {
        val animateTime = 2000L
        val scaleAnimation = ScaleAnimation(1f, 0.2f, 1f, 0.2f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f)

        val yOffset = destView.top - srcView.top
        val xOffset = destView.left - srcView.left
        val translateAnimation = TranslateAnimation(0f, xOffset.toFloat(), 0f, yOffset.toFloat())//平移动画  从0,0,平移到100,100

        val animations = AnimationSet(false)
        animations.addAnimation(scaleAnimation)
        animations.addAnimation(translateAnimation)
        animations.fillAfter = true
        animations.duration = animateTime
        animations.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationEnd(animation: Animation) {
                srcView.clearAnimation()
                srcView.visibility = INVISIBLE
            }

            override fun onAnimationStart(animation: Animation) {}
            override fun onAnimationRepeat(animation: Animation) {}
        })

        srcView.startAnimation(animations)
    }
}
